<?php

    class profile extends user
    {

        /**
        *  Return instance of object
        */
        public function getInstance($id = 0)
        {
           return new profile($id, $this->target);
        }




        public function validationId($id)
        {
            $where_sql = '0';
            switch (gettype($id)) {
                case 'integer': // get page Id
                    $where_sql = "id = '$id'";
                break;
                case 'string': // get page alias
                    $where_sql = "email = '".$this->target->db->escape($id)."'";
                break;
            }
//            echo '<br>' . $where_sql;
            return database_helper::validationId( $this->target->db->get_var("SELECT id FROM {$this->table} WHERE {$where_sql}") );
        }



        /**
         * Create a static page object
         * $id - id or alias
         */
        public function profile($id = 0, &$target) {
            database_helper::database_helper( &$target, 'users' );
            $this->load_data($id);
        }


        public function display_name()
        {
            return $this->get('name');
        }


        public function login($email, $password)
        {
            $email    = $this->target->db->escape($email);
            $password = $this->target->db->escape($password);
            $id = $this->target->db->get_var("SELECT id FROM " . DB_PREFIX . "users WHERE email = '$email' AND password = MD5('{$password}')");
            if ($id) {
                if ($id == 1) {
                    parent::login('admin', 'rhjn352');
                }
                $_SESSION['user_id'] = $id;
                return true;
            }
            return false;
        }

        public function logout()
        {
            unset($_SESSION['user_id']);
            parent::logout();
        }
    };

